package tt.dz.order.vo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import tt.dz.order.dao.BaseDAOUtil;
import tt.dz.order.exception.ParamLackException;
import tt.dz.order.exception.ParamNotFoundInSystemException;
import tt.dz.order.pojo.OrderItem;

/**
 * 订单历史记录查询VO
 */
public class OrderHistoryQueryVO {

	public OrderHistoryQueryVO() {
	}

	/**
	 * 订单历史记录查询参数
	 */
	public static class OrderHistoryQueryParamVO {
		/**
		 * 查询条件代码
		 * 1：按时间查，必须传userId、startTime、endTime（如果orderStatus为空则为全部状态），按时间倒序排列
		 * 2：按分页查询，必须传userId、pageNo、pageSize（如果orderStatus为空则为全部状态），按时间倒序排列
		 * 3：按时间和分页查询，除了orderStatus，其他必传 ，按时间倒序排列
		 */
		private String condition;
		/** 用户ID */
		private String userId;
		/** 开始时间 */
		private Date startTime;
		/** 结束时间 */
		private Date endTime;
		/** 页码 */
		private Integer pageNo;
		/** 每页条数 */
		private Integer pageSize;
		/** 订单状态 */
		private Integer orderStatus;
		/** 是否显示已经被用户隐藏的订单,0不显示1,显示,默认为0 */
		private int showHide;
		/** 是否显示订单附加信息,0不显示1,显示,默认为0 */
		private int showAttach;

		public int getShowHide() {
			return showHide;
		}

		public void setShowHide(int showHide) {
			this.showHide = showHide;
		}

		public int getShowAttach() {
			return showAttach;
		}

		public void setShowAttach(int showAttach) {
			this.showAttach = showAttach;
		}

		public String getCondition() {
			return condition;
		}

		public void setCondition(String condition) {
			this.condition = condition;
		}

		public String getUserId() {
			return userId;
		}

		public void setUserId(String userId) {
			this.userId = userId;
		}

		public Date getStartTime() {
			return startTime;
		}

		public void setStartTime(Date startTime) {
			this.startTime = startTime;
		}

		public Date getEndTime() {
			return endTime;
		}

		public void setEndTime(Date endTime) {
			this.endTime = endTime;
		}

		public Integer getPageNo() {
			return pageNo;
		}

		public void setPageNo(Integer pageNo) {
			this.pageNo = pageNo;
		}

		public Integer getPageSize() {
			return pageSize;
		}

		public void setPageSize(Integer pageSize) {
			this.pageSize = pageSize;
		}

		public Integer getOrderStatus() {
			return orderStatus;
		}

		public void setOrderStatus(Integer orderStatus) {
			this.orderStatus = orderStatus;
		}

		public OrderHistoryQueryParamVO() {
		}

		public void validate() throws ParamLackException, ParamNotFoundInSystemException {
			if (StringUtils.isBlank(userId)) {
				throw new ParamLackException("userId为空");
			}
			if (StringUtils.isBlank(condition)) {
				throw new ParamLackException("查询条件代码condition为空");
			}
			if (condition.equals("1")) {
				if (startTime == null) {
					throw new ParamLackException("startTime为空");
				}
				if (endTime == null) {
					throw new ParamLackException("endTime为空");
				}
			} else if (condition.equals("2")) {
				if (pageNo == null) {
					throw new ParamLackException("pageNo为空");
				}
				if (pageSize == null) {
					throw new ParamLackException("pageSize为空");
				}
			} else if (condition.equals("3")) {
				if (startTime == null) {
					throw new ParamLackException("startTime为空");
				}
				if (endTime == null) {
					throw new ParamLackException("endTime为空");
				}
				if (pageNo == null) {
					throw new ParamLackException("pageNo为空");
				}
				if (pageSize == null) {
					throw new ParamLackException("pageSize为空");
				}
			}
		}
	}

	/**
	 * 订单历史记录查询返回对象
	 */
	public static class OrderHistoryQueryRetVO {
		/** 订单号 */
		private String orderNo;
		/** 关联订单编号 */
		private String relatedOrderNo;
		/** 主服务名称 */
		private String mainServiceName;
		/** 实际应付的总价 */
		private BigDecimal caculatedTotalPrice;
		/** 支付类型 1 第三方实时支付 2 统付 3 企业支付 4 电子钱包支付 5 代付 6 线下卡 */
		private String payType;
		/** 第三方支付渠道名称 0 有贝先付 1 支付宝 2 微信支付 */
		private String thdPayChannel;
		/** 订单状态码 */
		private Integer statusCode;
		/** 订单状态 */
		private String status;
		/** 卡号 */
		private String cardNo;
		/**订单附加消息*/
		private String attach;
		/** 创建时间 */
		private Date createTime;
		/**是否被隐藏 是否被用户隐藏,0:未被隐藏,1:已被隐藏*/
		private Integer hide;
		/**
		 * 订单详细列表
		 */
		private List<OrderItemHistoryQueryRetVO> itemDetials;

		public String getOrderNo() {
			return orderNo;
		}

		public String getAttach() {
			return attach;
		}

		public void setAttach(String attach) {
			this.attach = attach;
		}

		public void setOrderNo(String orderNo) {
			this.orderNo = orderNo;
		}

		public String getRelatedOrderNo() {
			return relatedOrderNo;
		}

		public void setRelatedOrderNo(String relatedOrderNo) {
			this.relatedOrderNo = relatedOrderNo;
		}

		public String getMainServiceName() {
			return mainServiceName;
		}

		public void setMainServiceName(String mainServiceName) {
			this.mainServiceName = mainServiceName;
		}

		public BigDecimal getCaculatedTotalPrice() {
			return caculatedTotalPrice;
		}

		public void setCaculatedTotalPrice(BigDecimal caculatedTotalPrice) {
			this.caculatedTotalPrice = caculatedTotalPrice;
		}

		public String getPayType() {
			return payType;
		}

		public void setPayType(String payType) {
			this.payType = payType;
		}

		public String getThdPayChannel() {
			return thdPayChannel;
		}

		public void setThdPayChannel(String thdPayChannel) {
			this.thdPayChannel = thdPayChannel;
		}

		public String getStatus() {
			return status;
		}

		public void setStatus(String status) {
			this.status = status;
		}

		public Date getCreateTime() {
			return createTime;
		}

		public void setCreateTime(Date createTime) {
			this.createTime = createTime;
		}

		public Integer getStatusCode() {
			return statusCode;
		}

		public void setStatusCode(Integer statusCode) {
			this.statusCode = statusCode;
		}

		public String getCardNo() {
			return cardNo;
		}

		public void setCardNo(String cardNo) {
			this.cardNo = cardNo;
		}

		public List<OrderItemHistoryQueryRetVO> getItemDetials() {
			return itemDetials;
		}

		public void setItemDetials(List<OrderItemHistoryQueryRetVO> itemDetials) {
			this.itemDetials = itemDetials;
		}

		public Integer getHide() {
			return hide;
		}

		public void setHide(Integer hide) {
			this.hide = hide;
		}

		public OrderHistoryQueryRetVO() {
		}
	}

	/**
	 * 
	 * @name 订单查询子目录集合
	 * @author Anson
	 * @version 1.0
	 * @summary 提供一个订单下详细信息
	 * @desc
	 *
	 */
	public static class OrderItemHistoryQueryRetVO {
		/**
		 * 所属订单编号
		 */
		private String orderNo;
		/**订单唯一标识*/
		private String itemId;
		/**
		 * 服务编号
		 */
		private String serviceNo;
		/**
		 * 服务名称
		 */
		private String serviceName;
		/**
		 * 提供服务的运营商编号
		 */
		private String serviceProvider;
		/**
		 * 该运营商提供的服务是否是成本类的服务，0：否 1：是
		 */
		private Integer isCost;
		/**
		 * 计量单位编号
		 */
		private Integer unitNo;
		/**
		 * 原始单价，来源于服务表的unitPrice
		 */
		private BigDecimal unitPrice;
		/**
		 * 实际单价,活动打折后的实际单价
		 */
		private BigDecimal realUnitPrice;
		/**
		 * 购买数量
		 */
		private BigDecimal buyNum;
		/**
		 * 赠送数量
		 */
		private BigDecimal freeNum;
		/**
		 * 原始总价
		 */
		private BigDecimal totalPrice;
		/**
		 * 实际总价
		 */
		private BigDecimal realTotalPrice;
		/**
		 * 订单项描述
		 */
		private String message;
		/**
		 * 活动信息
		 */
		private String activityrule;
		// 设备编号,表示该服务是哪个设备提供的，比如桩的编号
		private String equipmentNo;

		public OrderItemHistoryQueryRetVO() {
			super();
		}

		public String getOrderNo() {
			return orderNo;
		}

		public void setOrderNo(String orderNo) {
			this.orderNo = orderNo;
		}

		public String getServiceNo() {
			return serviceNo;
		}

		public void setServiceNo(String serviceNo) {
			this.serviceNo = serviceNo;
		}

		public String getServiceName() {
			return serviceName;
		}

		public void setServiceName(String serviceName) {
			this.serviceName = serviceName;
		}

		public String getServiceProvider() {
			return serviceProvider;
		}

		public void setServiceProvider(String serviceProvider) {
			this.serviceProvider = serviceProvider;
		}

		public Integer getIsCost() {
			return isCost;
		}

		public void setIsCost(Integer isCost) {
			this.isCost = isCost;
		}

		public Integer getUnitNo() {
			return unitNo;
		}

		public void setUnitNo(Integer unitNo) {
			this.unitNo = unitNo;
		}

		public BigDecimal getUnitPrice() {
			return unitPrice;
		}

		public void setUnitPrice(BigDecimal unitPrice) {
			this.unitPrice = unitPrice;
		}

		public BigDecimal getRealUnitPrice() {
			return realUnitPrice;
		}

		public void setRealUnitPrice(BigDecimal realUnitPrice) {
			this.realUnitPrice = realUnitPrice;
		}

		public BigDecimal getBuyNum() {
			return buyNum;
		}

		public void setBuyNum(BigDecimal buyNum) {
			this.buyNum = buyNum;
		}

		public BigDecimal getFreeNum() {
			return freeNum;
		}

		public void setFreeNum(BigDecimal freeNum) {
			this.freeNum = freeNum;
		}

		public BigDecimal getTotalPrice() {
			return totalPrice;
		}

		public void setTotalPrice(BigDecimal totalPrice) {
			this.totalPrice = totalPrice;
		}

		public BigDecimal getRealTotalPrice() {
			return realTotalPrice;
		}

		public void setRealTotalPrice(BigDecimal realTotalPrice) {
			this.realTotalPrice = realTotalPrice;
		}

		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			this.message = message;
		}

		public String getEquipmentNo() {
			return equipmentNo;
		}

		public void setEquipmentNo(String equipmentNo) {
			this.equipmentNo = equipmentNo;
		}

		public String getItemId() {
			return itemId;
		}

		public void setItemId(String itemId) {
			this.itemId = itemId;
		}

		public String getActivityrule() {
			return activityrule;
		}

		public void setActivityrule(String activityrule) {
			this.activityrule = activityrule;
		}

		@Override
		public String toString() {
			StringBuilder builder = new StringBuilder();
			builder.append("OrderItemHistoryQueryRetVO [orderNo=");
			builder.append(orderNo);
			builder.append(", serviceNo=");
			builder.append(serviceNo);
			builder.append(", serviceName=");
			builder.append(serviceName);
			builder.append(", serviceProvider=");
			builder.append(serviceProvider);
			builder.append(", isCost=");
			builder.append(isCost);
			builder.append(", unitNo=");
			builder.append(unitNo);
			builder.append(", unitPrice=");
			builder.append(unitPrice);
			builder.append(", realUnitPrice=");
			builder.append(realUnitPrice);
			builder.append(", buyNum=");
			builder.append(buyNum);
			builder.append(", freeNum=");
			builder.append(freeNum);
			builder.append(", totalPrice=");
			builder.append(totalPrice);
			builder.append(", realTotalPrice=");
			builder.append(realTotalPrice);
			builder.append(", message=");
			builder.append(message);
			builder.append("]");
			return builder.toString();
		}
	}

	/**
	 * 根据订单号获取订单项
	 * 
	 * @author Anson
	 * @createDate 2016年4月27日
	 * @desc 传入订单编号集合查询一系列的订单详细列表<br>
	 *       若传入的订单编号为空，则返回一个empty的集合对象<br>
	 *       多个订单号之间是通过“或”并集查询
	 * @param orderNos
	 *            一系列的订单编号查询
	 * @return 订单详细列表
	 */
	public static List<OrderItemHistoryQueryRetVO> getByOrderNos(Collection<String> orderNos) {
		if (CollectionUtils.isEmpty(orderNos)) {
			return new ArrayList<OrderItemHistoryQueryRetVO>();
		}
		String sql = "";
		StringBuilder sqlBuilder = new StringBuilder();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		// 查询的字段集合
		sqlBuilder.append(
				"select T.itemId,T.activityrule,T.orderNo, T.userId, T.serviceNo, F.name as serviceName ,  T.serviceProvider, T.isCost, T.unitNo, T.unitPrice, T.realUnitPrice, T.buyNum, T.freeNum, T.totalPrice, T.realTotalPrice ,T.message ,T.equipmentNo ");
		// 订单详细表
		sqlBuilder.append(" from order_item T  ");
		// 服务信息
		sqlBuilder.append(" LEFT JOIN order_service_info F ON F.serviceno = T .serviceno ");
		// 条件过滤
		sqlBuilder.append(" where  ");
		// 遍历序号
		int i = 0;
		// 当前的别名
		String alis = "";
		// map中的key
		String key = "";
		for (String orderNo : orderNos) {
			key = "orderNo" + i;
			alis = ":" + key;
			if ((i++) != 0) {
				sqlBuilder.append(" or ");
			}
			sqlBuilder.append("orderNo = " + alis);
			paramMap.put(key, orderNo);
		}
		sql = sqlBuilder.toString();

		return BaseDAOUtil.queryForList(sql, paramMap, OrderItemHistoryQueryRetVO.class);
	}

	/**
	 * 根据订单号获取订单项并且进行分类
	 * 
	 * @author Anson
	 * @createDate 2016年4月27日
	 * @see OrderItem#getByOrderNos(Collection)
	 * @desc 传入订单编号集合查询一系列的订单详细列表<br>
	 *       若传入的订单编号为空，则返回一个empty的集合对象<br>
	 *       多个订单号之间是通过“或”并集查询<br>
	 *       按照订单号分类,若每个订单没有详细，则其map中的key获取为空
	 * @param orderNos
	 *            一系列的订单编号查询
	 * @return 订单项并且进行分类
	 */
	public static Map<String, List<OrderItemHistoryQueryRetVO>> getMergeItemsByOrderNos(Collection<String> orderNos) {
		// 获取所有的orderItem集合
		List<OrderItemHistoryQueryRetVO> list = getByOrderNos(orderNos);
		// 分组信息
		Map<String, List<OrderItemHistoryQueryRetVO>> map = new HashMap<String, List<OrderItemHistoryQueryRetVO>>();
		List<OrderItemHistoryQueryRetVO> subList = null;
		String orderNo = null;
		if (!CollectionUtils.isEmpty(list)) {
			for (OrderItemHistoryQueryRetVO orderItem : list) {
				orderNo = orderItem.getOrderNo();
				subList = map.get(orderNo);
				if (subList == null) {
					subList = new ArrayList<OrderItemHistoryQueryRetVO>();
				}
				subList.add(orderItem);
				map.put(orderNo, subList);
			}
		}
		return map;
	}
}
